package com.qbw.app.module.mine.activity;

import android.os.Bundle;
import android.view.View;

import com.qbw.app.R;
import com.qbw.app.databinding.MineActivityLanguageBinding;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.main.activity.MainActivity;
import com.qbw.app.utils.LanguageUtils;
import com.lib.util.SPUtils;

/**
 * @author QBW
 * @date 2019-10-20
 */
public class LanguageActivity extends CommonActivity<MineActivityLanguageBinding> implements View.OnClickListener{

    private int type; //0跟随系统  1 中文 2 英文
    @Override
    protected int getLayoutId() {
        return R.layout.mine_activity_language;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding.title.setListener(this);
        mBinding.rlChinese.setOnClickListener(this);
        mBinding.rlSystem.setOnClickListener(this);
        mBinding.rlEnglish.setOnClickListener(this);
        setLanguageView();
    }

    @Override
    public void onRightAreaClick() {
        super.onRightAreaClick();
        if(type == 0){
            LanguageUtils.setdefaultLanguage(this,"");
        }else if(type == 1){
            LanguageUtils.setdefaultLanguage(this,"zh");
        }else if(type == 2){
            LanguageUtils.setdefaultLanguage(this,"en");
        }
        finishAffinity();
        MainActivity.start(this);

    }


    private void setLanguageView(){
        String language = SPUtils.getInstance().getString("language");
        if("en".equals(language)){
            mBinding.tvSystem.setChecked(false);
            mBinding.tvChinese.setChecked(false);
            mBinding.tvEnglish.setChecked(true);
            type = 2;
        }else  if("zh".equals(language)){
            mBinding.tvSystem.setChecked(false);
            mBinding.tvChinese.setChecked(true);
            mBinding.tvEnglish.setChecked(false);
            type = 1;
        }else{
            mBinding.tvSystem.setChecked(true);
            mBinding.tvChinese.setChecked(false);
            mBinding.tvEnglish.setChecked(false);
            type = 0;
        }
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.rl_system) {
            mBinding.tvSystem.setChecked(true);
            mBinding.tvChinese.setChecked(false);
            mBinding.tvEnglish.setChecked(false);
            type = 0;
        }else if (vid == R.id.rl_chinese) {
            mBinding.tvSystem.setChecked(false);
            mBinding.tvChinese.setChecked(true);
            mBinding.tvEnglish.setChecked(false);
            type = 1;
        }else if (vid == R.id.rl_english) {
            mBinding.tvSystem.setChecked(false);
            mBinding.tvChinese.setChecked(false);
            mBinding.tvEnglish.setChecked(true);
            type = 2;
        }
    }
}
